﻿Imports System.IO
Imports System.Collections
Imports Id3Tag
Imports Id3Tag.HighLevel


Public Class MainForm

    Dim sMP3FolderPath As String
    Dim sFoldersList As New ArrayList(10)
    Dim sFilePathsList As New ArrayList(10)
    Dim sFileNamesList As New ArrayList(10)
    Dim sPlaylists As New ArrayList(10)
    Dim sPlaylistPaths As New ArrayList(10)
    Dim sMacFiles As New ArrayList(10)

    Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click

        Me.dgSelectFolder.ShowDialog()
        sMP3FolderPath = Me.dgSelectFolder.SelectedPath

        If Len(sMP3FolderPath) > 0 Then
            Me.lbDestination.Text = sMP3FolderPath
            Me.btnProcess.Enabled = True
        End If

    End Sub

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.btnProcess.Enabled = False
        Application.DoEvents()

    End Sub


    Private Sub btnProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProcess.Click

        'Build Folders Array
        AppendSubDirectories(sMP3FolderPath)

        Dim manager As New Id3TagManager
        Dim cont As Id3Tag.HighLevel.TagContainer
        Dim frame As Id3Frame.TextFrame
        Dim blankV1Tag As New Id3V1Tag

        'Process Arrays (Files first, folders after)
        Dim i, y As Integer


        'Rename ID3 Tags
        Me.pbBar.Minimum = 0
        Me.pbBar.Maximum = sFilePathsList.Count
        For i = 0 To sFilePathsList.Count - 1

            Me.lbDetails.Text = String.Format("Renaming ID3 Tag {0} of {1}", i + 1, sFilePathsList.Count)
            Me.pbBar.Value = i + 1
            Me.Refresh()

            'Remove ID3 Tag V1
            manager.WriteV1Tag(sFilePathsList(i) & "\" & sFileNamesList(i), blankV1Tag)

            cont = manager.ReadV2Tag(sFilePathsList(i) & "\" & sFileNamesList(i))

            For y = 0 To cont.Count - 1

                If cont.Item(y).Type = FrameType.Text Then
                    frame = cont.Item(y)
                    frame.Content = ReplaceLiteral(frame.Content.ToString)
                End If

            Next

            manager.WriteV2Tag(sFilePathsList(i) & "\" & sFileNamesList(i), cont)

        Next

        'Rename Files
        For i = 0 To sFilePathsList.Count - 1
            Me.lbDetails.Text = String.Format("Renaming File {0} of {1}", i + 1, sFilePathsList.Count)
            Me.pbBar.Value = i + 1
            Me.Refresh()
            File.Move(sFilePathsList(i) & "\" & sFileNamesList(i), sFilePathsList(i) & "\" & ReplaceLiteral(sFileNamesList(i)))
        Next


        'Deleting Mac Files
        Me.lbDetails.Text = "Deleting Invalid Files"
        Me.Refresh()
        Me.pbBar.Maximum = sMacFiles.Count
        For i = 0 To sMacFiles.Count - 1
            Me.lbDetails.Text = String.Format("Deleting File {0} of {1}", i + 1, sMacFiles.Count)
            Me.pbBar.Value = i + 1
            Me.Refresh()
            File.Delete(sMacFiles(i))
        Next


        'Updating Playlist Info
        Me.lbDetails.Text = "Updating Playlist Info"
        Me.Refresh()
        Me.pbBar.Maximum = sPlaylists.Count
        Dim oFile As System.IO.File
        Dim EntireFile As String
        Dim NewFileContent As String
        Me.Refresh()
        For i = 0 To sPlaylists.Count - 1
            Me.lbDetails.Text = String.Format("Updating Playlist {0} of {1}", i + 1, sPlaylists.Count)
            Me.pbBar.Value = i + 1
            Me.Refresh()

            EntireFile = oFile.ReadAllText(sPlaylistPaths(i) & "\" & sPlaylists(i))
            'Open Stream and Replace Text
            NewFileContent = ReplaceLiteral(EntireFile)
            oFile.WriteAllText(sPlaylistPaths(i) & "\" & sPlaylists(i), NewFileContent, System.Text.Encoding.UTF8)

            'Rename Playlist
            File.Move(sPlaylistPaths(i) & "\" & sPlaylists(i), sPlaylistPaths(i) & "\" & ReplaceLiteral(sPlaylists(i)))
        Next


        Dim NewFolder As String
        'Rename Folders
        Me.pbBar.Maximum = sFoldersList.Count
        Me.Refresh()
        For i = 0 To sFoldersList.Count - 1
            Me.lbDetails.Text = String.Format("Renaming Folder {0} of {1}", i + 1, sFoldersList.Count)
            Me.pbBar.Value = i + 1
            Me.Refresh()
            NewFolder = sFoldersList(i).ToString.Substring(0, sFoldersList(i).ToString.LastIndexOf("\")) & ReplaceLiteral(sFoldersList(i).ToString.Substring(sFoldersList(i).ToString.LastIndexOf("\")))
            If StrComp(sFoldersList(i), NewFolder) > 0 Then
                Directory.Move(sFoldersList(i), NewFolder)
            End If

        Next

        Me.lbDetails.Text = "Process Completed"


    End Sub

    Private Function ReplaceLiteral(ByVal OriginalString As String) As String

        Dim GreekChars() As String = {"A", "Ά", "α", "ά", "Β", "β", "Γ", "γ", "Δ", "δ", "Ε", "Έ", "ε", "έ", "Ζ", "ζ", "Η", "Ή", "η", "ή", "Θ", "θ", "Ι", "Ί", "Ϊ", "ι", "ί", "ϊ", "ΐ", "Κ", "κ", "Λ", "λ", "Μ", "μ", "Ν", "ν", "ΟΥ", "ΟΎ", "Ου", "Ού", "ου", "ού", "Ο", "Ό", "ο", "ό", "Π", "π", "Ρ", "ρ", "Σ", "σ", "ς", "Τ", "τ", "Υ", "Ύ", "Ϋ", "υ", "ύ", "ϋ", "ΰ", "Φ", "φ", "Χ", "χ", "Ψ", "ψ", "Ω", "Ώ", "ω", "ώ"}
        Dim LatinChars() As String = {"A", "A", "a", "a", "V", "v", "G", "g", "D", "d", "E", "E", "e", "e", "Z", "z", "H", "H", "i", "i", "Th", "th", "I", "I", "I", "i", "i", "i", "i", "K", "k", "L", "l", "M", "m", "N", "n", "OY", "OY", "Ou", "Ou", "ou", "ou", "o", "o", "o", "o", "P", "p", "R", "r", "S", "s", "s", "T", "t", "Y", "Y", "Y", "y", "y", "y", "y", "F", "f", "X", "x", "PS", "ps", "W", "W", "w", "w"}

        Dim i As Integer

        For i = 0 To GreekChars.Length - 1

            OriginalString = OriginalString.Replace(GreekChars(i), LatinChars(i))

        Next

        Return OriginalString

    End Function

    Public Sub AppendFilesToArray(ByVal sPath As String)

        Dim di As New IO.DirectoryInfo(sPath)
        Dim arFi As IO.FileInfo() = di.GetFiles("*.mp3")
        Dim fi As IO.FileInfo

        For Each fi In arFi
            sFilePathsList.Add(fi.DirectoryName)
            sFileNamesList.Add(fi.Name)
        Next
    End Sub

    Public Sub AppendSubDirectories(ByVal sPath As String)
        Dim currDirectory As New IO.DirectoryInfo(sPath)

        sFoldersList.Add(currDirectory.FullName)
        AppendFilesToArray(currDirectory.FullName)
        GetMacFiles(currDirectory.FullName)
        AppendPlaylists(currDirectory.FullName)

        For Each subDirectory As IO.DirectoryInfo In currDirectory.GetDirectories
            AppendSubDirectories(subDirectory.FullName)

        Next
    End Sub

    Public Sub GetMacFiles(ByVal sPath As String)
        Dim di As New IO.DirectoryInfo(sPath)
        Dim arFi As IO.FileInfo() = di.GetFiles(".*")
        Dim fi As IO.FileInfo

        For Each fi In arFi
            sMacFiles.Add(fi.FullName)
        Next
    End Sub

    Public Sub AppendPlaylists(ByVal sPath As String)
        Dim di As New IO.DirectoryInfo(sPath)
        Dim arFi As IO.FileInfo() = di.GetFiles("*.m3u")
        Dim fi As IO.FileInfo

        For Each fi In arFi
            sPlaylistPaths.Add(fi.DirectoryName)
            sPlaylists.Add(fi.Name)
        Next

    End Sub



End Class
